﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeView
{
    public partial class 高级控件应用类型 : Form
    {
        public 高级控件应用类型()
        {
            InitializeComponent();
        }

      

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void 加入子节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddchildNode();
        }

        private void 高级控件应用类型_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            TreeNode tem = new TreeNode("根节点");
            treeView1.Nodes.Add(tem);
        }
        private void AddchildNode()
        {
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("请选择一个节点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (textBox1.Text != "")
                {
                    TreeNode tmp;
                    tmp = new TreeNode(textBox1.Text);
                    treeView1.SelectedNode.Nodes.Add(tmp);
                    treeView1.SelectedNode = tmp;
                    treeView1.ExpandAll();
                }
                else
                {
                    MessageBox.Show("TextBox组件必须填入节点名称！", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }
        private void AddParent()
        {
            try
            {
                if (treeView1.SelectedNode == null)
                {
                    MessageBox.Show("请选择一个节点", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    if (textBox1.Text != "")
                    {
                        TreeNode tmp;
                        tmp = new TreeNode(textBox1.Text);
                        treeView1.SelectedNode.Parent.Nodes.Add(tmp);
                        treeView1.ExpandAll();
                    }
                    else
                    {
                        MessageBox.Show("TextBox组件必须输入节点名称！", "提示信息",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            catch
            {
                TreeNode tem = new TreeNode("根节点");
                treeView1.Nodes.Add(tem);
            }
        }

       

        private void button2_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode = treeView1.Nodes[0];
            treeView1.SelectedNode.ExpandAll();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode = treeView1.Nodes[0];
            treeView1.SelectedNode.Collapse();

        }

        private void 加入兄弟节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddParent();
        }

        private void 删除节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.Nodes.Count == 0)
                treeView1.SelectedNode.Remove();
            else
                MessageBox.Show("请先删除此节点中的子节点！", "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                contextMenuStrip1.Show(this, new Point(e.X, e.Y));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Expand();
        }
    }
}